package com.zhhtech.zhh.ui.activity.main

import android.view.View
import androidx.core.view.isGone
import androidx.core.view.isVisible
import com.zhhtech.zhh.api.api
import com.zhhtech.zhh.api.core.launchUI
import com.zhhtech.zhh.api.core.toData
import com.zhhtech.zhh.common.UserCache
import com.zhhtech.zhh.databinding.ActivityInviteCodeBinding
import com.zhhtech.zhh.ui.api.ApiObserver
import com.zhhtech.zhh.ui.base.BaseActivity
import com.zhhtech.zhh.ui.ext.toastMessageLong
import com.zhhtech.zhh.ui.ext.value
import com.zhhtech.zhh.ui.utils.variableView

class InviteCodeActivity : BaseActivity<ActivityInviteCodeBinding>() {
    override fun initView() {
        bindViewClick(vb.buttonSubmit)
        variableView {
            addTextView(vb.etInviterCode)
            addOnValidationResultListener(vb.buttonSubmit)
        }
        UserCache.getUserInfoBean().apply {
            vb.rl1.isVisible = isBindInviter
            vb.rl2.isVisible = isBindInviter
            vb.buttonSubmit.isGone = isBindInviter
            vb.etInviterCode.isGone = isBindInviter

            vb.tvBindInviterPhone.text = bindInviterPhone
            vb.tvBindInviterCode.text = bindInviterCode
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.buttonSubmit -> {
                launchUI({
                    api.setInviter(vb.etInviterCode.value).toData()
                    "绑定成功".toastMessageLong()
                    finish()
                }, ApiObserver(mThis))
            }
        }
    }
}